package com.weishenai.customerassistant.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weishenai.customerassistant.common.Result;
import com.weishenai.customerassistant.entity.Substation;
import com.weishenai.customerassistant.service.SubstationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@Api(tags = "管理系统_供电所信息")
@RequestMapping("/substation")
public class SubstationController extends BaseController{

    @Autowired
    private SubstationService service;

    @PreAuthorize("hasAuthority('power:station:list')")
    @ApiOperation(("查询所有的供电所信息"))
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "第几页"),
            @ApiImplicitParam(name = "pageSize", value = "每一页的数量"),
    })
    @GetMapping("/selectAll/{pageNum}/{pageSize}")
    public Object selectAll(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        IPage<Substation> selectAll = service.selectAll(pageNum, pageSize);
        return Result.success(selectAll);
    }

    @ApiOperation(("查询所有的供电所信息"))
    @GetMapping("/selectAll")
    public Object selectAll() {
        return Result.success( service.selectAll());
    }

    @PreAuthorize("hasAuthority('power:station:list:edit')")
    @ApiOperation("更改一条供电所信息")
    @PostMapping("/update")
    private Object update(@RequestBody Substation substation) {
        boolean result = service.updateById(substation);
        if (result) {
            return Result.success();
        } else return Result.failed("失败");
    }

    @PreAuthorize("hasAuthority('power:station:list:delete')")
    @ApiOperation("删除一条供电所信息")
    @ApiImplicitParam(name = "id", value = "供电所id")
    @PostMapping("/delById/{id}")
    public Object delSubstationById(@PathVariable("id") Integer id) {
        Integer integer = service.delSubstation(id);
        if (integer > 0) {
            return Result.success();
        } else return Result.failed("失败");
    }

    @PreAuthorize("hasAuthority('power:station:list')")
    @ApiOperation(("根据关键字查询所有的供电所信息"))
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "第几页"),
            @ApiImplicitParam(name = "pageSize", value = "每一页的数量"),
            @ApiImplicitParam(name = "keyWord", required = true, value = "模糊查询关键字")
    })
    @GetMapping("/selectAll/{pageNum}/{pageSize}/{keyWord}")
    public Object selectByKeyWord(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize, @PathVariable("keyWord") String keyword) {
        IPage<Substation> iPage = service.selectByKeyWord(pageNum, pageSize, keyword);
        return Result.success(iPage);
    }

    @PreAuthorize("hasAuthority('power:station:list:add')")
    @ApiOperation("增加一条供电所信息")
    @PostMapping("/insert")
    public Object insert(@RequestBody Substation substation) {
        if (substation == null || substation.getSubstationName() == null) return Result.failed("供电站名字不能为空！");
        Integer result = service.insert(substation);
        return result > 0 ? Result.success() : Result.failed("失败");
    }
}
